import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.shopx.sys.dto.CpTreeDTO;
import com.shopx.sys.dto.CpTreeShowDTO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

public class BeanToJson {
    public static List<CpTreeShowDTO> convertToTree(List<CpTreeDTO> list) {
        if (list == null || list.isEmpty()) {
            return new ArrayList<>();
        }

        // 将 List 转换为 Map，方便根据 parCpId 查找子节点
        Map<String, List<CpTreeDTO>> map = new HashMap<>();
        for (CpTreeDTO dto : list) {
            if(dto.getParCpId() == null || dto.getParCpId().isEmpty()){
                dto.setParCpId("0");
            }
            if (!map.containsKey(dto.getParCpId())) {
                map.put(dto.getParCpId(), new ArrayList<>());
            }
            map.get(dto.getParCpId()).add(dto);
        }

        // 查找根节点（parCpId 为 null 或为空的节点）
        List<CpTreeDTO> rootNodes = map.get("0");

        List<CpTreeShowDTO> result = new ArrayList<>();
        if (rootNodes != null) {
            for (CpTreeDTO rootNode : rootNodes) {
                CpTreeShowDTO treeNode = new CpTreeShowDTO();
                treeNode.setId(rootNode.getCpId());
                treeNode.setTitle(rootNode.getCpName());
                treeNode.setChildren(buildChildren(rootNode.getCpId(), map));
                result.add(treeNode);
            }
        }
        return result;
    }

    private static List<CpTreeShowDTO> buildChildren(String parentId, Map<String, List<CpTreeDTO>> map) {
        List<CpTreeDTO> childrenNodes = map.get(parentId);
        if (childrenNodes == null || childrenNodes.isEmpty()) {
            return null;
        }

        List<CpTreeShowDTO> children = new ArrayList<>();
        for (CpTreeDTO childNode : childrenNodes) {
            CpTreeShowDTO treeNode = new CpTreeShowDTO();
            treeNode.setId(childNode.getCpId());
            treeNode.setTitle(childNode.getCpName());
            List<CpTreeShowDTO> grandChildren = buildChildren(childNode.getCpId(), map);
            treeNode.setChildren(grandChildren);
            children.add(treeNode);
        }
        return children;
    }


    public static void main(String[] args) {
        // 构造示例数据
        List<CpTreeDTO> list = new ArrayList<>();
        CpTreeDTO jiangxi = new CpTreeDTO();
        jiangxi.setCpId("1");
        jiangxi.setCpName("江西");
        jiangxi.setParCpId(null);
        jiangxi.setCpLevel(1);
        list.add(jiangxi);

        CpTreeDTO nanchang = new CpTreeDTO();
        nanchang.setCpId("1000");
        nanchang.setCpName("南昌");
        nanchang.setParCpId("1");
        nanchang.setCpLevel(2);
        list.add(nanchang);


        CpTreeDTO qingshanhu = new CpTreeDTO();
        qingshanhu.setCpId("10001");
        qingshanhu.setCpName("青山湖区");
        qingshanhu.setParCpId("1000");
        qingshanhu.setCpLevel(3);
        list.add(qingshanhu);

        CpTreeDTO gaoxin = new CpTreeDTO();
        gaoxin.setCpId("10002");
        gaoxin.setCpName("高新区");
        gaoxin.setParCpId("1000");
        gaoxin.setCpLevel(3);
        list.add(gaoxin);

        // 进行转换
        List<CpTreeShowDTO> tree = convertToTree(list);

        // 输出结果
        System.out.println(JSON.toJSONString(tree,true));
    }


}

